package databases

import (
	"hulk-api/conf"
	"database/sql"
)

type Instance string

func (i Instance) DbConn( dbName string) (dbconn *sql.DB , err error)  {
	dbuser ,err := conf.Cfg.GetValue(string(i),"username")
	checkErr(err)
	dbpassword ,err := conf.Cfg.GetValue(string(i),"password")
	checkErr(err)
	dbhost ,err := conf.Cfg.GetValue(string(i),"hostname")
	checkErr(err)
	dbPort ,err := conf.Cfg.GetValue(string(i),"port")
	checkErr(err)

	dataSourceUrl := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbPort+ ")/" + dbName + "?parseTime=true&charset=utf8mb4"

	dbconn, err = sql.Open("mysql", dataSourceUrl)
	checkErr(err)
	err = dbconn.Ping()
	checkErr(err)
	return
}

